$(function(){
  $.dataGridEasyUI.initTable({
	  pageId:"#adminListPage",
	  gridId:".adminList",
	  url:getBasePath()+'/adminUser/getAdminList.do',
	  toolbarId:"#adminListPage .tb_toolbar",
	  columns:[[
		        {field:'uuid',checkbox:true},
		        /*{field:'deptName',title:'所属部门/职位',width:150,formatter:function(value, row, index) {
		        	if(null!=row.positionName){
		        		return row.companyName+"<br/>|—"+row.deptName+"<br/>&nbsp;&nbsp;&nbsp;|—"+row.positionName;
		        	}
		        	return row.companyName+"<br/>|—"+row.deptName;
				}},*/
		        {field:'userName',title:'用户名',width:120,formatter: function(value,row,index){
	            	var loadUrl = "javascript:loadAdminDetail(\""+row.uuid+"\")";
	            	return "<a href="+loadUrl+"><font color=#0000FF><b><u>"+value+"</u></b></font></a>";
			    }},
			    {field:'realName',title:'真实姓名',width:120},
		        {field:'groupName',title:'所属角色',width:120},	      
		        {field:'mobileTel',title:'手机号码',width:100},
		        {field:'email',title:'邮箱',width:120},
		        {field:'loginCount',title:'登录次数',width:90},
		        {field:'locked',title:'是否被锁',width:50,formatter:function(value, row, index) {
		        	if(value==0){
		        		return "<font color=green><b>未锁</b></font>";
		        	}else{
		        		return "<font color=red><b>已锁</b></font>";
		        	}
				}},
		        {field:'editTime',title:'编辑时间',width:150,formatter:function(value, row, index) {
		        	return formatByMillisecond(value, "yyyy-MM-dd HH:mm:ss");
				}}
		     ]]
  });
  
  //新增按钮绑定事件
  $("#addDataLink").bind("click",function(){
	  $.dataGridEasyUI.initEditWindow({
		  url:getBasePath()+"manage/xtsz/editAdmin.jsp",
		  title:"添加后台管理员信息",
		  actionType:"add"
	  });
  });
  
  //更新按钮绑定事件
  $("#updateDataLink").bind("click",function(){
	  $.dataGridEasyUI.initEditWindow({
		  url:getBasePath()+"manage/xtsz/editAdmin.jsp",
		  title:"编辑后台管理员信息",
		  actionType:"update"
	  });
  });
  
  //批量删除
  $("#batchDeleteLink").bind("click",function(){
	  $.dataGridEasyUI.batchDeleteData({
		  url:getBasePath()+"/adminUser/batchDelAdminUser.do",
	  });
  });
  
  //单个删除
  $("#singleDeleteLink").bind("click",function(){
	  $.dataGridEasyUI.batchDeleteData({
		  url:getBasePath()+"/adminUser/delAdminUser.do",
	  });
  });
  
  //显示详情
  $("#showDetailLink").bind("click",function(){
	  $.dataGridEasyUI.initEditWindow({
		  url:getBasePath() + "manage/xtsz/adminDetail.jsp",
		  title:"展示详情",
		  actionType:"detail",
		  width:650,
		  height:350
	  });
  });
  
  /**
   * 重置用户密码为8个8
   */
  $("#resetPsdLink").bind("click",function(){
		//验证是否登录--开始
	  	if(!checkLoginStatus()){return;}
	      //验证是否登录--结束
	  	
	  	var curUserName = $("#curUserName").val();
	  	var ids = [];
		var rows = tableObject.datagrid('getSelections');
		if(rows.length<1){
			$.messager.alert('系统提示','请选中需要操作的用户！','error');
			return;
		}
		
		//获取datagrid选中行
		for (var i = 0; i < rows.length; i++) {
			ids.push(rows[i].id);
		}
		ids = ids.join(',');
		$.messager.confirm('确认对话框', '此操作将会将选中用户的密码重置为默认密码8个8,请确认是否继续操作？', function(flag){
		  if (flag){
			$.post(getBasePath()+"/adminUser/resetPassword.do",{ids:ids},
		    	function(jsonData){
					if(jsonData.code=="00"){
						$.messager.alert('系统提示',jsonData.msg,'success');
					}else{
						$.messager.alert('系统提示',jsonData.msg,'error');
					}
		    	},"json");
			}
		});
  });
  loadCompanyTree();
})

//检索数据
function seachData(){
   var searChObj = new Object(); 
   var formId = "searchForm";
   $("#"+formId).find("input").each(function(i){
	   searChObj[$(this).attr("name")] = $(this).val();
   });
   
   $("#"+formId).find("select").each(function(i){
	   searChObj[$(this).attr("name")] = $(this).val();
   });
   tableObject.datagrid({
	     queryParams:searChObj
   })
}
  
  //显示详情页
  function loadAdminDetail(userUUID){
	  $.dataGridEasyUI.initEditWindow({
		  url:getBasePath() + "manage/xtsz/adminDetail.jsp?userUUID=" + userUUID,
		  title:"展示详情",
		  actionType:"other",
		  width:650,
		  height:350
	  });
  }

/**
 * 获取企业树数据
 * @returns
 */
function loadCompanyTree(){
	  var daohangArea = $('#adminListPage .daohangArea');
	  var url = getBasePath()+"company/getCompanyList4Tree.do"
	  daohangArea.tree({    
	        url:url,
	        method: 'post',
    		animate: true,
    		lines:true,
    		//单击某个节点触发的事件
    		onClick: function(node){
    			//按企业筛选
				tableObject.datagrid({
        		  queryParams: {
        			 companyUUID:node.id
    		      }
        	    });
    		},
    });  
}